package com.css.fxfzmhwz.base.dict.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzmhwz.base.dict.repository.entity.DictEntity;
import com.css.fxfzmhwz.base.dict.service.DictService;
import com.css.fxfzmhwz.base.dict.viewobjects.DictQueryParams;
import com.css.fxfzmhwz.base.response.RestResponse;
import com.css.fxfzmhwz.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/base/dicts")
@Slf4j
public class DictController {
    @Autowired
    DictService dictService;

    @Autowired
    HttpServletRequest request;

    @PostMapping("/dictExcel")
    public RestResponse uploadFile(@RequestParam("file") MultipartFile file, HttpServletResponse response){
        RestResponse restResponse = null;
        try {
            String returnMsg = dictService.uploadFile(file,response);
            restResponse = RestResponse.succeed(returnMsg);
        }catch (Exception e){
            String errorMessage = "字典项列表信息导入失败!";
            log.error(errorMessage,e);
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }

    @GetMapping("dictExport")
    public void dictExport(HttpServletResponse response){
        try{
            dictService.exportDict(response);
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
    }

    @GetMapping("queryDicts")
    public RestResponse queryDicts(HttpServletRequest request, DictQueryParams queryParams) {
        RestResponse response = null;
        JSONObject jsonObject = new JSONObject();
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            jsonObject = dictService.queryDicts(queryParams,curPage,pageSize);
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            log.error("查询字典失败!",e);
            response=RestResponse.fail("查询字典失败!");
        }
        return response;
    }

    @GetMapping("{id}")
    public RestResponse getDict(@PathVariable String id) {
        RestResponse response = null;
        DictEntity de = null;
        try{
             de = dictService.getDict(id);
            response = RestResponse.succeed(de);
        }catch (Exception e){
            log.error("获取字典失败!",e);
            response=RestResponse.fail("获取字典失败!");
        }
        return response;
    }

    @GetMapping("/getValidDictItemsByDictCode")
    public RestResponse getValidDictItemsByDictCode(String dictCode) {
        RestResponse response = null;
        try{
            response = RestResponse.succeed(dictService.getValidDictItemsByDictCode(dictCode));
        }catch (Exception e){
            log.error("获取字典失败!",e);
            response=RestResponse.fail("获取字典失败!");
        }
        return response;
    }
    @PostMapping
    public RestResponse saveDict(@RequestBody DictEntity dictEntity) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            dictService.saveDict(dictEntity);
            json.put("message", "新增字典成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            log.error("新增字典失败!",e);
            json.put("message", "新增字典失败!");
            response=RestResponse.succeed(json);
        }
        return response;

    }
    @PutMapping
    public RestResponse updateDict(@RequestBody DictEntity dictEntity)  {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            dictService.updateDict(dictEntity);
            json.put("message", "修改字典成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            log.error("修改字典失败!",e);
            json.put("message", "修改字典失败!");
            response=RestResponse.succeed(json);
        }
        return response;
    }
    @DeleteMapping("{ids}")
    public RestResponse deleteDicts(@PathVariable("ids") List<String> ids) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            dictService.deleteDicts(ids);
            json.put("message", "删除字典成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            log.error("删除字典失败!",e);
            json.put("message", "删除字典失败!");
            response=RestResponse.succeed(json);
        }
        return response;
    }
    @GetMapping("updateDictsType/{ids}/{type}")
    public RestResponse updateDictsType(@PathVariable("ids") List<String> ids, @PathVariable("type") String type) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        if("1".equals(type)){
            try{
                dictService.updateDicts(ids,type);
                json.put("message", "激活字典成功!");
                response = RestResponse.succeed(json);

            }catch (Exception e){
                log.error("激活字典失败!",e);
                json.put("message", "激活字典失败!");
                response=RestResponse.succeed(json);
            }
        }else{
            try{
                dictService.updateDicts(ids,type);
                json.put("message", "禁用字典成功!");
                response = RestResponse.succeed(json);
            }catch (Exception e){
                log.error("禁用字典失败!",e);
                json.put("message", "禁用字典失败!");
                response=RestResponse.succeed(json);
            }
        }
        return response;
    }
    @GetMapping("validateDictCode")
    public RestResponse validateDictCode(@RequestParam String dictId, @RequestParam String dictCode) {
        RestResponse response = null;
        boolean isValid = false;
        try{
            isValid = dictService.validateDictCode(dictId,dictCode);
            response = RestResponse.succeed(isValid);
        }catch (Exception e){
            log.error("校验字典编码唯一性失败!",e);
            response=RestResponse.fail("校验字典编码唯一性失败!");
        }
        return response;
    }
}